#include "visualVideo.h"
visualVideo::visualVideo(const char * _videoFileName, const char * _retFileName)
{
  if( _videoFileName == NULL )
    {
      ERROR("video file is NULL") ;
      exit(-1) ; 
    }
  if( _retFileName == NULL )
    {
      ERROR("result file is NULL") ;
      exit(-1) ;
    }
  display(_videoFileName,_retFileName) ;
}
visualVideo::~visualVideo()
{
}
void visualVideo::display(const char * _videoFileName , const char * _retFileName)
{
  CvCapture * capture = cvCaptureFromFile(_videoFileName) ;
  if( capture == NULL )
    {
      INFO2("video file name",_videoFileName) ;
      ERROR("can't open the video") ;
      exit(-1) ;
    }
  std::ifstream fileInStream(_retFileName) ;
  if( !fileInStream.is_open() ) 
    {
      INFO2("file name can't open",_retFileName) ;
      exit(-1) ;
    }
  int* labelArray = new int[2000] ;
  memset(labelArray,0,sizeof(int)*2000 ) ;
  int framesNum , label ;
  while( fileInStream >> framesNum >> label )
    {
      labelArray[framesNum] += label ;
    }
  cvNamedWindow("检测视频",1) ;
  CvFont* font ;
  cvInitFont(font,CV_FONT_HERSHEY_SIMPLEX,0.5f,0.5f) ;
  int index = 0 ;
  IplImage* imagePtr ;
  while( ( imagePtr = cvQueryFrame(capture)) != NULL )
    {
      std::string text ;
      if( labelArray[index] > 0 )
	    text = std::string("label:violent") ;
	  else text = std::string("label:normal") ;
      cvPutText(imagePtr,text.c_str(),cvPoint(0,20),font,cvScalar(255,0,0)) ;      
      cvShowImage("检测视频",imagePtr) ;
      cvWaitKey(30) ;
      ++index ;
    }
  cvReleaseCapture(&capture) ;
  cvDestroyWindow("检测视频") ;
  delete[] labelArray ;
}
